package com.liuhan.house.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuhan.house.dao.UserMapper;
import com.liuhan.house.pojo.User;
import com.liuhan.house.service.MailService;
import com.liuhan.house.service.UserService;
import com.liuhan.house.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MailService mailService;


    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addUser(User user) {
        if (user == null) {
            return false;
        }
        // 设置新注册用户为不可使用状态
        user.setUserStatus(0);
        // 用户密码MD5加密
        user.setUserPassword(MD5Util.MD5EncodeUtf8(user.getUserPassword()));
        mailService.registerNotify(user.getUserEmail());
        int resultCount = userMapper.insertSelective(user);
        if (resultCount > 0) {
            return true;
        }
        return false;
    }

    /**
     * 验证邮箱激活
     * @param key
     * @return
     */
    @Override
    public boolean enable(String key) {
        return mailService.enable(key);
    }

    /**
     * 校验用户名密码是否正确
     * @param userEmail
     * @param password
     * @return
     */
    @Override
    public User verifyUser(String userEmail, String password) {
        int resultCount = userMapper.verifyUser(userEmail, MD5Util.MD5EncodeUtf8(password));
        // 查询到正确结果
        if (resultCount > 0) {
            return userMapper.selectByUserNameAndPassword(userEmail, MD5Util.MD5EncodeUtf8(password));
        }
        return null;
    }

    /**
     * 更新个人信息

     */
    @Override
    public void updateUserInformation(String username, String phone, String aboutme, String email) {
        userMapper.updateUserInfo(username, phone, aboutme, email);
    }

    /**
     * 根据邮件查询用户信息
     * @param email
     * @return
     */
    @Override
    public User getUserByEmail(String email) {
        return userMapper.getUserByEmail(email);
    }

    /**
     * 修改密码

     * @param newPassword
     * @param email
     */
    @Override
    public void editorPassword(String newPassword, String email) {
        userMapper.editorPassword(MD5Util.MD5EncodeUtf8(newPassword), email);
    }

    /**
     * 根据旧密码和邮件查询用户
     * @param userEmail
     * @param password
     * @return
     */
    @Override
    public User getUserByEmailAndPassword(String userEmail, String password) {
        return userMapper.getUserByEmailAndPassword(userEmail, MD5Util.MD5EncodeUtf8(password));

    }

    /**
     * 根据userId查询用户
     * @param userId
     * @return
     */
    @Override
    public User selectByPrimaryKey(Integer userId) {

        return userMapper.selectByPrimaryKey(userId);
    }

    /**
     * 查询中介集合
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<User> getAgencyList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.getAgencyList();
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }

    @Override
    public User getInfoById(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

}
